﻿Imports System.Windows.Forms
Imports System.Diagnostics.Process

Public Class MainActivity
    Dim WorkDirList(5) As String
    Dim AppNameList(5) As String
    Dim m_sFilePath As String
    Dim m_sFiles() As String

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        txtSearchString.Text = My.Settings.StringSearch
        cmbAppName.Items.Add(My.Settings.app1)
        cmbAppName.Items.Add(My.Settings.app2)
        cmbAppName.Items.Add(My.Settings.app3)
        cmbAppName.Items.Add(My.Settings.app4)
        cmbAppName.Items.Add(My.Settings.app5)
        cmbWorkDir.Items.Add(My.Settings.wd1)
        cmbWorkDir.Items.Add(My.Settings.wd2)
        cmbWorkDir.Items.Add(My.Settings.wd3)
        cmbWorkDir.Items.Add(My.Settings.wd4)
        cmbWorkDir.Items.Add(My.Settings.wd5)
        txtPreArguments.Text = My.Settings.prePend
        txtPostArgument.Text = My.Settings.postPend
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Dim i As Integer = 0
        My.Settings.StringSearch = txtSearchString.Text
        My.Settings.prePend = txtPreArguments.Text
        My.Settings.postPend = txtPostArgument.Text
        My.Settings.wd1 = WorkDirList(0)
        My.Settings.wd2 = WorkDirList(1)
        My.Settings.wd3 = WorkDirList(2)
        My.Settings.wd4 = WorkDirList(3)
        My.Settings.wd5 = WorkDirList(4)
        My.Settings.app1 = AppNameList(0)
        My.Settings.app2 = AppNameList(1)
        My.Settings.app3 = AppNameList(2)
        My.Settings.app4 = AppNameList(3)
        My.Settings.app5 = AppNameList(4)
        Me.Close()
    End Sub

    Private Sub btnSelectFolder_Click(sender As System.Object, e As System.EventArgs) Handles btnSelectFolder.Click
        Populate()
    End Sub

    Private Sub btnAppBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnAppBrowse.Click
        Dim FileOpen As New OpenFileDialog
        Dim i As Integer = 1
        Dim strTemp(5) As String
        FileOpen.Filter = "Executable Files (*.exe)|*.exe|" _
             & "All Files(*.*)|"
        FileOpen.FilterIndex = 0
        If FileOpen.ShowDialog() = DialogResult.OK Then
            ReDim strTemp(5)
            strTemp(0) = FileOpen.FileName
            cmbAppName.SelectedText = FileOpen.FileName
            While i < 5
                strTemp(i) = AppNameList(i - 1)
                i += 1
            End While
            i = 0
            While i < 5
                If strTemp(i) <> "" Then
                    AppNameList(i) = strTemp(i)
                End If
                i += 1
            End While

        Else
            'Nothing
        End If
    End Sub

    Private Sub btnConvert_Click(sender As System.Object, e As System.EventArgs) Handles btnConvert.Click
        'ReDim Preserve m_sFiles(m_sFiles.Length)
        Dim l_length As Integer = m_sFiles.Length
        Dim i As Integer = 0
        Dim l_sPreArguments As String = txtPreArguments.Text
        Dim l_sPostArguments As String = txtPostArgument.Text
        'strip extension
        If chkRemoveExt.Checked = True Then
            l_sPostArguments = Replace(lstFiles.Text, txtSearchString.Text, txtExtension.Text)
        End If
        While i < m_sFiles.Length
            Dim strQuotedFile As String
            If i < lstFiles.Items.Count Then
                If lstFiles.Items.Item(i) <> Nothing Then
                    strQuotedFile = """" + lstFiles.Items.Item(i) + """"
                    Convert(strQuotedFile, cmbAppName.Text, l_sPreArguments, l_sPostArguments, m_sFilePath)
                Else
                    MessageBox.Show("Can not convert nothing.  Please select at least one file to convert.", "NULL value", MessageBoxButtons.OK)
                End If
            End If
            i += 1
        End While
    End Sub

    Private Sub Populate()
        'Browse for Folder, set path
        ReDim m_sFiles(0)
        Dim BrowseFolder As New FolderBrowserDialog
        Dim j As Integer = 0
        Dim i As Integer = 1
        lstFiles.Items.Clear()
        If BrowseFolder.ShowDialog() = DialogResult.OK Then
            ReDim strTemp(5)
            strTemp(0) = BrowseFolder.SelectedPath
            While i < 5
                strTemp(i) = WorkDirList(i - 1)
                i += 1
            End While
            i = 0
            While i < 5
                WorkDirList(i) = strTemp(i)
                i += 1
            End While

            m_sFilePath = BrowseFolder.SelectedPath
            cmbWorkDir.Text = BrowseFolder.SelectedPath

            If chkRecursive.Checked = False Then

                For Each foundFile As String In My.Computer.FileSystem.GetFiles _
              (BrowseFolder.SelectedPath, _
               FileIO.SearchOption.SearchTopLevelOnly, txtSearchString.Text)
                    ReDim Preserve m_sFiles(j)
                    m_sFiles(j) = foundFile
                    lstFiles.Items.Add(foundFile, True)
                    j += 1
                Next

            ElseIf chkRecursive.Checked = True Then
                For Each foundFile As String In My.Computer.FileSystem.GetFiles _
             (BrowseFolder.SelectedPath, _
              FileIO.SearchOption.SearchAllSubDirectories, txtSearchString.Text)
                    ReDim Preserve m_sFiles(j)
                    m_sFiles(j) = foundFile
                    lstFiles.Items.Add(foundFile, True)
                    j += 1
                Next

            End If
        ElseIf BrowseFolder.ShowDialog() = DialogResult.Cancel Then
            'Insert Error handling! [Abort, Retry or Epic?]
            Return
        End If
    End Sub
    Private Function Convert(ByVal l_sFile As String, ByVal l_sCommand As String, l_sPreArguments As String, l_sPostArguments As String, l_sWorkDir As String)
        Dim psi As New Process
        psi.StartInfo.WorkingDirectory = l_sWorkDir
        psi.StartInfo.FileName = l_sCommand
        psi.StartInfo.Arguments = l_sPreArguments + l_sFile + l_sPostArguments
        psi.StartInfo.UseShellExecute = True
        psi.Start()
        psi.WaitForExit()
    End Function
End Class